package com.ad.backstage.service.impl;

import com.ad.backstage.enums.LevelEnum;
import com.ad.backstage.mapper.MerchantMapper;
import com.ad.backstage.mapper.PfCategoryMapper;
import com.ad.backstage.mapper.PfCategorySubMapper;
import com.ad.backstage.model.Merchant;
import com.ad.backstage.model.PfCategory;
import com.ad.backstage.model.PfCategorySub;
import com.ad.backstage.service.CategoryService;
import com.ad.payload.dto.trade.UpdateCategoryByMerchantIdDto;
import com.ad.payload.model.IdDto;
import com.ad.payload.vo.trade.ParentCategoryVo;
import com.ad.payload.vo.trade.SubCategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:dxgong
 * @日期 2021/6/15 16:02
 * @注释
 */
@Service
public class CategoryServiceImpl implements CategoryService{

    @Autowired
    private PfCategoryMapper pfCategoryMapper;

    @Autowired
    private PfCategorySubMapper pfCategorySubMapper;


    public static Map<String, Long> FIRST_CATEGORY_MAP = new HashMap<>();

    @Autowired
    private MerchantMapper merchantMapper;

    /**
     * 查询一级行业
     */
    @Override
    public List<ParentCategoryVo> queryFirstCategory() {
        QueryWrapper<PfCategory> queryWrapper = new QueryWrapper();
        queryWrapper.orderByAsc("id");
        List<PfCategory> pfCategories = pfCategoryMapper.selectList(queryWrapper);

        List<ParentCategoryVo> rtnList = new ArrayList<>();
        pfCategories.forEach(item -> {
            ParentCategoryVo parentCategoryVo = new ParentCategoryVo();
            parentCategoryVo.setName(item.getCategoryName());
            parentCategoryVo.setId(item.getId());

            rtnList.add(parentCategoryVo);

            FIRST_CATEGORY_MAP.put(item.getCategoryName(), item.getId());
        });
        return rtnList;
    }

    /**
     * 根据trade_id询二级行业
     *
     * @param idDto
     */
    @Override
    public List<SubCategoryVo> querySecondCategory(IdDto idDto) {
        QueryWrapper<PfCategorySub> queryWrapper = new QueryWrapper();
        queryWrapper.orderByAsc("id");
        queryWrapper.eq("level", LevelEnum.LEVEL_2.getCode());
        queryWrapper.eq("trade_id", idDto.getId());

        List<PfCategorySub> pfCategorySubs = pfCategorySubMapper.selectList(queryWrapper);

        List<SubCategoryVo> rtnList = new ArrayList<>();

        pfCategorySubs.forEach(item -> {
            SubCategoryVo subCategoryVo = new SubCategoryVo();
            subCategoryVo.setId(item.getId());
            subCategoryVo.setName(item.getSubName());
            rtnList.add(subCategoryVo);
        });

        return rtnList;
    }

    /**
     * 根据一级行业名称查询id
     *
     * @param name
     */
    @Override
    public ParentCategoryVo queryFirstCategoryByName(String name) {
        QueryWrapper<PfCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_name", name);

        PfCategory pfCategory = pfCategoryMapper.selectOne(queryWrapper);
        ParentCategoryVo parentCategoryVo = new ParentCategoryVo();
        parentCategoryVo.setId(pfCategory.getId());
        parentCategoryVo.setName(pfCategory.getCategoryName());
        return parentCategoryVo;
    }

    /**
     * 根据二级行业名称查询id
     *
     * @param name
     */
    @Override
    public SubCategoryVo querySecondCategoryByName(String name) {
        QueryWrapper<PfCategorySub> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level", LevelEnum.LEVEL_2.getCode());
        queryWrapper.eq("sub_name", name);

        PfCategorySub pfCategorySub = pfCategorySubMapper.selectOne(queryWrapper);

        SubCategoryVo subCategoryVo = new SubCategoryVo();
        subCategoryVo.setName(pfCategorySub.getSubName());
        subCategoryVo.setId(pfCategorySub.getId());

        return subCategoryVo;
    }

    /**
     * 根据商家id更新商家行业id
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCategoryByMerchantId(UpdateCategoryByMerchantIdDto dto) {
        Merchant merchant = new Merchant();
        merchant.setId(Long.valueOf(dto.getId()));
        merchant.setCategoryId(Long.valueOf(dto.getParentId()));
        merchant.setCategorySubId(Long.valueOf(dto.getSubId()));
        merchantMapper.updateById(merchant);
    }


}
